//
//  LocationViewController.swift
//  worldexchange
//
//  Created by xionghao on 2019/1/8.
//  Copyright © 2019 xionghao. All rights reserved.
//

import UIKit
import MJRefresh

class LocationViewController: QMUICommonViewController {
    @IBOutlet weak var tableView: UITableView!
    
    
    var shops:[ShopItem] = [ShopItem]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupHeaderView()
        self.setupTableView()
        // Do any additional setup after loading the view.
    }
    

    func setupTableView() {
        self.tableView.rowHeight = 160
        self.tableView.separatorStyle = .none
        self.tableView.register(UINib(nibName: "ShopTableViewCell", bundle: nil), forCellReuseIdentifier: "cell-item")
        
        self.tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            self.loadShops()
        })
    }
    
    func setupHeaderView() {
        
        let topLogoView =  TopLogoView.loadView()
        topLogoView.titleLabel.text = LanguageHelper.getString(key: "CFBundleDisplayName")
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: topLogoView)
        let telBtn = UIBarButtonItem.qmui_item(with: #imageLiteral(resourceName: "icon_tel"), target: self, action: #selector(telClick))
        let convertBtn = UIBarButtonItem.qmui_item(with: #imageLiteral(resourceName: "icon_covert"), target: self, action: #selector(convertClick))
        
        self.navigationItem.rightBarButtonItems = [telBtn,convertBtn]
        
    }

    @objc func convertClick(){
        let tabVc =  self.navigationController?.tabBarController as? AppTabBarViewController
        tabVc?.showConvert()
    }

    @objc func telClick(){
        let tabVc =  self.navigationController?.tabBarController as? AppTabBarViewController
        tabVc?.telClick()
    }
    
    func loadShops() {
        provider.rx.request(WE.shop()).filterSuccessfulStatusCodes().mapString().subscribe(onSuccess: { (result) in
            if let shops = [ShopItem].deserialize(from: result) as? [ShopItem] {
                appShopItems.removeAll()
                appShopItems.append(contentsOf: shops)
                self.tableView.reloadData()
                self.tableView.mj_header.endRefreshing()
            }
        }) { (_) in
            
        }
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
extension LocationViewController :QMUITableViewDelegate,QMUITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return appShopItems.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell-item") as! ShopTableViewCell
        cell.item = appShopItems[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = appShopItems[indexPath.row]
        let shopVc = ShopDetailViewController()
        shopVc.hidesBottomBarWhenPushed = true
        shopVc.shopItem  = item
        self.navigationController?.pushViewController(shopVc, animated: true)
    }
    
    
}
